﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace QRCodeSample
{
    public class MyListView : ListView
    {
        public MyListView()
        {
            SetStyle(ControlStyles.DoubleBuffer
                | ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.AllPaintingInWmPaint, true);
            UpdateStyles();
        }

        public void InvokeAddItem(ListViewItem item)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(() =>
                {
                    Items.Add(item);
                }));
            }
            else
            {
                Items.Add(item);
            }
        }

        public void InvokeSetCell(int itemIndex, int subItemIndex, string text)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(() =>
                {
                    Items[itemIndex].SubItems[subItemIndex].Text = text;
                }));
            }
            else
            {
                Items[itemIndex].SubItems[subItemIndex].Text = text;
            }
        }

        public void InvokeEnsureVisible(int itemIndex)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(() =>
                {
                    Items[itemIndex].EnsureVisible();
                }));
            }
            else
            {
                Items[itemIndex].EnsureVisible();
            }
        }
    }
}
